package com.smartleanx.module.hrm.service.employee;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.smartleanx.module.hrm.controller.admin.employee.work.EmployeeWorkExperienceRespVO;
import com.smartleanx.module.hrm.controller.admin.employee.work.EmployeeWorkExperienceSaveReqVO;
import com.smartleanx.module.hrm.dal.dataobject.employee.EmployeeDO;
import com.smartleanx.module.hrm.dal.dataobject.employee.EmployeeEducationExperienceDO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;

import com.smartleanx.module.hrm.dal.dataobject.employee.EmployeeWorkExperienceDO;
import com.smartleanx.framework.common.pojo.PageResult;
import com.smartleanx.framework.common.util.object.BeanUtils;

import com.smartleanx.module.hrm.dal.mysql.employee.EmployeeWorkExperienceMapper;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import static com.smartleanx.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.smartleanx.module.hrm.enums.ErrorCodeConstants.*;

/**
 * 员工工作经历 Service 实现类
 *
 * @author tengcj
 */
@Service
@Validated
public class EmployeeWorkExperienceServiceImpl implements EmployeeWorkExperienceService {

    @Resource
    private EmployeeWorkExperienceMapper employeeWorkExperienceMapper;
    @Resource
    private EmployeeService employeeService;

    @Override
    public Long createEmployeeWorkExperience(EmployeeWorkExperienceSaveReqVO createReqVO) {
        // 插入
        EmployeeWorkExperienceDO employeeWorkExperience = BeanUtils.toBean(createReqVO, EmployeeWorkExperienceDO.class);
        // 校验时间有效性
        validateWorkExperienceTime(createReqVO);
        // 校验员工是否存在
        EmployeeDO employeeDO = employeeService.getEmployeeById(createReqVO.getEmployeeId());
        if (employeeDO == null) {
            throw exception(EMPLOYEE_NOT_EXISTS);
        }
        List<EmployeeWorkExperienceDO> experienceList = getEmployeeWorkExperienceList(createReqVO.getEmployeeId());
        employeeWorkExperience.setSort(CollUtil.isEmpty(experienceList) ? 0 : experienceList.get(0).getSort() + 1);
        employeeWorkExperienceMapper.insert(employeeWorkExperience);
        // 返回
        return employeeWorkExperience.getId();
    }

    @Override
    public void updateEmployeeWorkExperience(EmployeeWorkExperienceSaveReqVO updateReqVO) {
        // 校验存在
        validateEmployeeWorkExperienceExists(updateReqVO.getId());
        // 校验时间有效性
        validateWorkExperienceTime(updateReqVO);
        // 校验员工是否存在
        EmployeeDO employeeDO = employeeService.getEmployeeById(updateReqVO.getEmployeeId());
        if (employeeDO == null) {
            throw exception(EMPLOYEE_NOT_EXISTS);
        }
        // 更新
        EmployeeWorkExperienceDO updateObj = BeanUtils.toBean(updateReqVO, EmployeeWorkExperienceDO.class);
        employeeWorkExperienceMapper.updateById(updateObj);
    }

    public void validateWorkExperienceTime(EmployeeWorkExperienceSaveReqVO vo) {
        if (Objects.nonNull(vo.getWorkStartTime()) && Objects.nonNull(vo.getWorkEndTime())) {
            if (vo.getWorkStartTime().compareTo(vo.getWorkEndTime()) > 0) {
                throw exception(WORK_START_TIME_UNLATER_END_TIME);
            }
        }
    }

    @Override
    public void deleteEmployeeWorkExperience(Long id) {
        // 校验存在
        validateEmployeeWorkExperienceExists(id);
        // 删除
        employeeWorkExperienceMapper.deleteById(id);
    }

    private void validateEmployeeWorkExperienceExists(Long id) {
        if (employeeWorkExperienceMapper.selectById(id) == null) {
            throw exception(EMPLOYEE_WORK_EXPERIENCE_NOT_EXISTS);
        }
    }

    @Override
    public EmployeeWorkExperienceDO getEmployeeWorkExperience(Long id) {
        return employeeWorkExperienceMapper.selectById(id);
    }

    @Override
    public List<EmployeeWorkExperienceDO> getEmployeeWorkExperienceList(Long employeeId) {
        return employeeWorkExperienceMapper.getEmployeeWorkExperienceList(employeeId);
    }

    @Override
    public void deleteByEmployeeId(Long employeeId) {
        employeeWorkExperienceMapper.delete(new LambdaQueryWrapper<EmployeeWorkExperienceDO>(){{
            eq(EmployeeWorkExperienceDO::getEmployeeId, employeeId);
        }});
    }
}
